package com.hy.proxy.server.controller;

import com.hy.proxy.core.entity.Proxy;
import com.hy.proxy.server.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: hy
 * @Date: Created in 2019/10/1/21:59
 * @Description:
 * @Version: 1.0
 */
@RestController()
@RequestMapping("/api")
@Slf4j
public class ProxyController {

    @Resource
    private RedisUtils redisUtils;

    @GetMapping(value = "/ssr")
    public String listSsr() {
        return redisUtils.findProxyByProxyType(Proxy.ProxyType.SSR);
    }

    @GetMapping(value = "/ss")
    public String listSs() {
        return redisUtils.findProxyByProxyType(Proxy.ProxyType.SS);
    }

    @GetMapping(value = "/v2")
    public String listV2() {
        return redisUtils.findProxyByProxyType(Proxy.ProxyType.V2RAY);
    }

    @GetMapping(value = "/filessr", produces = "application/octet-stream; charset=utf-8")
    public ResponseEntity<String> fileSsr() {
        String base64Ssr = redisUtils.findProxyByProxyType(Proxy.ProxyType.SSR);

        return ResponseEntity
                .ok()
                .header("Content-Disposition", "attachment; filename=" + "ssr" + ".txt")
                .contentLength(base64Ssr.length())
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(base64Ssr);
    }

    @GetMapping(value = "/filess", produces = "application/octet-stream; charset=utf-8")
    public ResponseEntity<String> fileSs() {
        String base64Ssr = redisUtils.findProxyByProxyType(Proxy.ProxyType.SS);
        return ResponseEntity
                .ok()
                .header("Content-Disposition", "attachment; filename=" + "ss" + ".txt")
                .contentLength(base64Ssr.length())
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(base64Ssr);
    }

}
